﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace Cipher.Web.MVC {
    public class IntAttribute : ValidationAttribute {
        public IntAttribute() { }

        public bool AllowEmpty { get; set; }

        public override bool IsValid(object value) {
            if(value == null && AllowEmpty)
                return true;

            string strValue = value.ToString();

            if(strValue == string.Empty && AllowEmpty)
                return true;

            int val;
            return int.TryParse(strValue, out val);
        }
    }
}